package org.makcro.sms.services.chat.web.client.mvc.view;

import java.util.List;

import org.makcro.sms.services.chat.base.dto.HistoryItem;
import org.makcro.sms.services.chat.web.client.AppEvents;
import org.makcro.sms.services.chat.web.client.widgets.AnswerDialog;
import org.makcro.sms.services.chat.web.shared.dto.HistoryItemDto;

import com.extjs.gxt.ui.client.core.Template;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Params;

public class AnswerView extends View {

	private AnswerDialog	answerDialog;
	private Template		answerStatsTemplate;

	public AnswerView(Controller controller) {
		super( controller );
	}

	@Override
	protected void initialize() {
		super.initialize();
		answerDialog = new AnswerDialog();
		/*answerDialog.addListener( Events.Hide, new Listener<WindowEvent>() {

			@Override
			public void handleEvent(WindowEvent be) {
				Dispatcher.get().dispatch( AppEvents.ANSWER_DIALOG_HIDDEN );
			}

		} );*/
	}

	@Override
	protected void handleEvent(AppEvent event) {
		EventType type = event.getType();

		if ( type == AppEvents.PREPARE_ANSWER ) {
			onPrepareAnswer( event );
		}

		else if ( type == AppEvents.ANSWER_TEXT_CHANGED ) {
			onAnswerTextChanged( event );
		}
		else if (type == AppEvents.ANSWER_SENT) {
			onAnswerSent(event);
		}
		else if (type == AppEvents.ANSWER_ABOUT_TO_BE_SENT) {
			onAnswerAboutToBeSent(event);
		}
		else if(type == AppEvents.ANSWER_SENDING_FAILED){
			onAnswerSendingFailed(event);
		}
	}

	private void onAnswerSendingFailed(AppEvent event) {
		answerDialog.getButtonBar().enable();
		answerDialog.hide();
	}

	private void onAnswerAboutToBeSent(AppEvent event) {
		answerDialog.getButtonBar().disable();
	}

	private void onAnswerSent(AppEvent event) {
		answerDialog.getButtonBar().enable();
		answerDialog.hide();
	}

	public Template getAnswerStatsTemplate() {
		if ( answerStatsTemplate == null ) {
			answerStatsTemplate = new Template( "Broj karaktera u odgovoru: {charactetCount}" );
		}
		return answerStatsTemplate;
	}

	private void onAnswerTextChanged(AppEvent event) {
		String answerText = answerDialog.getAnswerTextPanel().getTxtAnswerText().getValue();
		Integer answerTextLength = answerText.length();

		Params templateParams = new Params( "charactetCount", answerTextLength );
		String appledTemplate = getAnswerStatsTemplate().applyTemplate( templateParams );
		answerDialog.getAnswerTextPanel().getLblAnswerStats().setText( appledTemplate );
	}

	private void onPrepareAnswer(AppEvent event) {
		answerDialog.reset();

		List<HistoryItemDto> historyItems = event.<List<HistoryItemDto>> getData();

		ListStore<HistoryItemDto> store = answerDialog.getStore();

		store.add( historyItems );

		answerDialog.show();
	}

}
